package com.naml.d_methods;

/**
 * @author marin
 * @version 1.0
 * @date 2025/4/28 20:11
 * @desc 演示中断线程的操作-并不是停止线程的意思，实际是唤醒正在睡眠的线程
 */
@SuppressWarnings("all")
public class ThreadInterrupt01 {
    public static void main(String[] args) {
        ThreadInterrupt thread = new ThreadInterrupt();
        thread.setName("章三");
        thread.start();

        for (int i = 0; i < 10 ; i++) {
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + ":>>>" + i);
                if (i == 5){
                    //中断线程
                    thread.interrupt();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.err.println("不给吃了...");
        thread.stop();
    }
}

@SuppressWarnings("all")
class ThreadInterrupt extends Thread {

    @Override
    public void run() {
        while (true) {
            try {
                for (int i = 1; i <= 10; i++) {
                    System.out.println(Thread.currentThread().getName() +":吃了>>>"+ i +"个包子...");
                }
                System.err.println(Thread.currentThread().getName() + ":开始休息20s...");
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                System.err.println(Thread.currentThread().getName() + ":被唤醒继续吃包子...");
            }
        }
    }
}